﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ParkingLot.Business.Entitys;

namespace ParkingLot.Utility.CollectionEntity
{
    public class CollectionFactory
    {
        private static int SaveCount = 20;

        /// <summary>
        /// 存储所有的 全部的Collection集合
        /// </summary>
        private static Dictionary<Guid, object> m_DictCollection = new Dictionary<Guid, object>();

        /// <summary>
        /// 无则创建有则获取CommonCollection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T CreateOrGetCollection<T>() where T : CommonCollection
        {
            Guid tGuid = typeof(T).GUID;
            Func<T> createFunc = () =>
            {
                T t = Activator.CreateInstance<T>();
                //if (m_DictCollection.Count >= SaveCount)
                //{
                //    KeyValuePair<Guid, object> keyValuePair = m_DictCollection.ElementAt(0);
                //    m_DictCollection.Remove(keyValuePair.Key);
                //}
                //m_DictCollection.Add(tGuid, t);
                return t;
            };
            return BufferObjectHelp.CreateOrGetT<T>(tGuid, createFunc);
        }

        /// <summary>
        /// 获取包含[全部] 选项的 CommonCollection 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetWithAllCollection<T>() where T : CommonCollection
        {
            Guid tGuid = typeof(T).GUID;
            if (!m_DictCollection.ContainsKey(tGuid))
            {
                T t = Activator.CreateInstance<T>();
                t.AddAll();
                if (m_DictCollection.Count >= SaveCount)
                {
                    KeyValuePair<Guid, object> keyValuePair = m_DictCollection.ElementAt(0);
                    m_DictCollection.Remove(keyValuePair.Key);
                }
                m_DictCollection.Add(tGuid, t);
            }

            return m_DictCollection[tGuid] as T;
        }

    }
}
